//
//  PresentationModalTransition.m
//  CKApp
//
//  Created by CC on 2020/9/4.
//  Copyright © 2020 This Moment. All rights reserved.
//

#import "PresentationModalTransition.h"
#import "LcFoundation.h"
#import "PresentationShowAnimation.h"
#import "PresentationDismissAnimation.h"
#import "PresentationController.h"

@interface PresentationModalTransition () <UIViewControllerTransitioningDelegate>
@property(nonatomic, strong) PresentationShowAnimation* showAnimation;
@property(nonatomic, strong) PresentationDismissAnimation* dismissAnimation;
@property(nonatomic, strong) OnClickedHandler dismissHandler;

@end

@implementation PresentationModalTransition

- (instancetype)init {
  self = [super init];
  if (self) {
    [self commonInit];
  }
  return self;
}

- (void)viewDidLoad {
  [super viewDidLoad];
  self.view.backgroundColor = [UIColor clearColor];
  self.showAnimation = [[PresentationShowAnimation alloc] init];
  self.dismissAnimation = [[PresentationDismissAnimation alloc] init];
}

- (void)commonInit {
  self.modalPresentationStyle = UIModalPresentationCustom;
  self.transitioningDelegate = self;
}

- (void)presentationModalShow:(void (^)(void))completion {
  [[UIViewController currentViewController] presentViewController:self
                                                         animated:YES
                                                       completion:completion];
}

- (void)presentationModalDismiss:(void (^)(void))completion {
  self.presentationCtrl = nil;
  [self.view endEditing:YES];
  if (self.dismissHandler != nil) {
    self.dismissHandler(self);
  } else {
    [self dismissViewControllerAnimated:YES completion:completion];
  }
}

#pragma mark - UIViewControllerTransitioningDelegate
- (UIPresentationController*)
presentationControllerForPresentedViewController:(UIViewController*)presented
                        presentingViewController:(UIViewController*)presenting
                            sourceViewController:(UIViewController*)source {
  self.presentationCtrl =
      [[PresentationController alloc] initWithPresentedViewController:presented
                                             presentingViewController:presenting];
  self.presentationCtrl.isGaussianBlur = self.isGaussianBlur;
  return self.presentationCtrl;
}

- (id<UIViewControllerAnimatedTransitioning>)
animationControllerForPresentedController:(UIViewController*)presented
                     presentingController:(UIViewController*)presenting
                         sourceController:(UIViewController*)source {
  return self.showAnimation;
  ;
}

- (id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:
    (UIViewController*)dismissed {
  return self.dismissAnimation;
}

@end
